# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
 
@File:tuan_quanwen1_0.py
@Time:2024/8/13 21:13

@Motto:不积跬步无以至千里，不积小流无以成江海！
"""
def read_file_and_create_dict(filename):
    # 初始化一个空字典
    tuan_dict = {}

    # 初始化变量
    key = None
    value = []

    # 使用with语句安全地打开文件
    with open(filename, 'r', encoding='utf-8') as file:
        # 逐行读取文件
        for line in file:
            # 去除行首尾的空白字符
            clean_line = line.strip()
            # 检查是否包含“彖传原文”
            if "彖传原文" in clean_line:
                # 如果已经存在key，则将当前value列表添加到字典中
                if key is not None:
                    tuan_dict[key] = value
                # 更新key为当前行，并重置value列表
                key = clean_line
                value = []
            else:
                # 如果当前行不是“彖传原文”，添加到value列表
                if key is not None:
                    value.append(clean_line)

        # 循环结束后，添加最后一个value列表到字典中
        if key is not None and value:
            tuan_dict[key] = value

    return tuan_dict

# 调用函数并传入文件名
filename = 'yi/tuan_quanwen.txt'
tuan_dict = read_file_and_create_dict(filename)

# 打印字典中的所有项
for key, value in tuan_dict.items():
    print(f"{key}")
    for line in value:
        print(f"{line}")